<?php
namespace backend\modules\payment\models;

use common\libs\Constants;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\activeRecords\TPay;

/**
 * PaySearch represents the model behind the search form of `app\models\activeRecords\TPay`.
 */
class PaySearch extends TPay
{
    public $spread_key;
    public $spread_key_1;
    public $spread_key_2;
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['pay_id', 'pay_state', 'pay_game_id', 'pay_server_id', 'pay_time', 'coupon_id'], 'integer'],
            [['spread_key','pay_time','spread_key_1','spread_key_2','pay_order_no', 'pay_order_no_cooperate', 'pay_type', 'pay_mode_id', 'pay_user', 'pay_tel', 'pay_game_user', 'pay_game_role', 'pay_ip', 'card_id', 'pay_game_name', 'pay_server_name', 'remark'], 'safe'],
            [['pay_money', 'pay_money_with_rate'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = TPay::find();

        // add conditions that should always apply here
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        // grid filtering conditions
        $query->andFilterWhere([
            'pay_id' => $this->pay_id,
            'pay_state' => Constants::Status_Success,
            'pay_game_id' => $this->pay_game_id,
            'pay_server_id' => $this->pay_server_id,
            'pay_money' => $this->pay_money,
            'pay_money_with_rate' => $this->pay_money_with_rate,
            'coupon_id' => $this->coupon_id,
        ]);

        if(!empty($this->pay_time)){
            $time = explode(' - ',$this->pay_time);
            $query->andFilterWhere(['between','pay_time',strtotime($time[0]),strtotime($time[1])+Constants::ONE_DAY-1]);
        }

        $query->andFilterWhere(['like', 'pay_order_no', $this->pay_order_no])
            ->andFilterWhere(['like', 'pay_order_no_cooperate', $this->pay_order_no_cooperate])
            ->andFilterWhere(['like', 'pay_type', $this->pay_type])
            ->andFilterWhere(['like', 'pay_mode_id', $this->pay_mode_id])
            ->andFilterWhere(['like', 'pay_user', $this->pay_user])
            ->andFilterWhere(['like', 'pay_tel', $this->pay_tel])
            ->andFilterWhere(['like', 'pay_game_user', $this->pay_game_user])
            ->andFilterWhere(['like', 'pay_game_role', $this->pay_game_role])
            ->andFilterWhere(['like', 'pay_ip', $this->pay_ip])
            ->andFilterWhere(['like', 'card_id', $this->card_id])
            ->andFilterWhere(['like', 'pay_game_name', $this->pay_game_name])
            ->andFilterWhere(['like', 'pay_server_name', $this->pay_server_name])
            ->andFilterWhere(['like', 'remark', $this->remark])
            ->joinWith('user')
            ->andFilterWhere(['like', 't_user.spread_key_1', Constants::ANCHOR])
            ->andWhere('length(t_user.spread_key)>0');
            $auth = Yii::$app->authManager;
            $roleGuildManager = Constants::ROLE_GUILD_MANAGER;
            $roleGuildChannel = Constants::ROLE_GUILD_CHANNEL;
            if(!empty($auth->getRolesByUser(Yii::$app->user->getId())[$roleGuildManager])){
                $query->andFilterWhere(['like', 't_user.spread_key', Yii::$app->user->getIdentity()->parent]);
                $query->andFilterWhere(['like', 't_user.spread_key_2', $this->spread_key_2]);
            }elseif(!empty($auth->getRolesByUser(Yii::$app->user->getId())[$roleGuildChannel])){
                $query->andFilterWhere(['like', 't_user.spread_key_2', Yii::$app->user->getIdentity()->position]);
            }else{
                $query->andFilterWhere(['like', 't_user.spread_key', $this->spread_key]);
                $query->andFilterWhere(['like', 't_user.spread_key_2', $this->spread_key_2]);
            }

        return $dataProvider;
    }
}
